import os
import time
import requests

# 请求头
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0',
}

# 创建统一的图片保存文件夹
if not os.path.exists('images'):
    os.makedirs('images')

for i in range(1, 2):
    # 获取响应的请求数据
    json_data = {
        'size': 24,
        'current': i,
        'sort': 0,
        'category': 0,
        'resolution': 0,
        'color': 0,
        'categoryId': 0,
        'ratio': 0,
    }

    res = requests.post('https://api.zzzmh.cn/v2/bz/v3/getData', headers=headers, json=json_data)

    for j in range(1, 25):
        try:
            res_img = requests.get(f"https://api.zzzmh.cn/v2/bz/v3/getUrl/{res.json()['data']['list'][j]['i']}29")
            if res_img.status_code == 200:
                with open(f'images/image_{i}_{j}.jpg', 'wb') as file:
                    file.write(res_img.content)
                    print(f'写入第 {i} 页第 {j} 张图片')
            else:
                time.sleep(2)
                res_img = requests.get(f"https://api.zzzmh.cn/v2/bz/v3/getUrl/{res.json()['data']['list'][j]['i']}19")
                with open(f'images/image_{i}_{j}.jpg', 'wb') as file:
                    file.write(res_img.content)
                    print(f'写入第 {i} 页第 {j} 张图片')
        except Exception as e:
            print(e)